<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="pdictiing-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="Serch">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictis" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
				  @row-dblclick="hankddirticdbCilck" v-model="filters.keyWord" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" label="编号" type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="产品类型" width="120" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编码" width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
			@current-change="handleCurrentChange"
			:page-size="pageSize"
			:current-page="currentPage"
			:total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/修改界面-->
		<el-dialog :title="title" :visible.sync="dictiFormVisible" :close-on-click-modal="false">
			<el-form :model="dictiForm" label-width="80px" :rules="dictiFormRules" ref="dictiForm">
				<el-form-item label="产品类型" prop="name">
					<el-input v-model="dictiForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编码" prop="sn">
					<el-input v-model="dictiForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="邮箱" prop="email">-->
				<!--<el-input v-model="dictiForm.email" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item label="年龄" prop="age">-->
				<!--<el-input v-model="dictiForm.age" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item label="电话" prop="phone">-->
				<!--<el-input v-model="dictiForm.phone" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item label="盐值" prop="salt">-->
				<!--<el-input v-model="dictiForm.salt" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item label="状态">-->
				<!--<el-radio-group v-model="dictiForm.state">-->
						<!--<el-radio class="radio" :label="1">启用</el-radio>-->
						<!--<el-radio class="radio" :label="0">禁用</el-radio>-->
					<!--</el-radio-group>-->
				<!--</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dictiFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="dictiSubmit" :loading="dictiLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--  抽屉 -->
		<el-drawer
				title="字典明细"
				:visible.sync="thedrawers.drawer"
				:direction="direction">
			<!--  字典明细列表-->
			<el-table :data="dictistais" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
					   v-model="filters.keyWord" style="width: 100%;">
				<el-table-column type="selection" width="55">
				</el-table-column>
				<el-table-column prop="id" label="编号" type="index" width="60">
				</el-table-column>
				<el-table-column prop="name" label="类型对应名称" width="120" sortable>
				</el-table-column>
				<el-table-column prop="types_id" label="类型编码" width="120" sortable>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template scope="scope">
						<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--字典明细工具条-->
			<el-col :span="24" class="toolbar">
				<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
				<el-pagination layout="prev, pager, next"
							   @current-change="handledritiCurrentChange"
							   :page-size="detaipageSize"
							   :current-page="detaicurrentPage"
							   :total="detaitotal" style="float:right;">
				</el-pagination>
			</el-col>
		</el-drawer>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDictiListPage, removeDicti, batchRemoveDicti, editDicti, dictiDicti } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyWord: ''
				},
				dictis: [],//字典数据
                dictisid:null,//字典id 用于传递到后端查询
                dictistais: [],//字典明细数据
				total: 0,
                pageSize: 2,
                currentPage: 1,

				detaitotal: 0,//字典明细总数
                detaipageSize: 3,//字典明细显示条数
                detaicurrentPage: 1,//字典明细当前页数

                listLoading: false,
				sels: [],//列表选中列

				dictiFormVisible: false,//新增、修改界面是否显示
				dictiLoading: false,//等待框
				dictiFormRules: {//数据校验
					name: [
						{ required: true, message: '请输入产品类型', trigger: 'blur' }
					],
                    sn: [
                        { required: true, message: '请输入编码', trigger: 'blur' }
                    ]
				},
				//新增修改界面数据
				dictiForm: {
				    id:null,
					name: '',
					sn: '',
                    // state: 0,
				},
				//抽屉弹出
                thedrawers:{
				    drawer:false
				}
			}
		},
		methods: {
		    //双击弹出抽屉
            hankddirticdbCilck(row){
                this.thedrawers.drawer = row;
                //将我当前行的id取出 用于传递到后端查询
                this.dictisid = row.id;
                //当我点击弹出抽屉的时候查询品牌对应的七匹狼等
                this.getDictistAll();
			},
            //获取字典明细最新当前页数，刷新页面
            handledritiCurrentChange(val) {
                this.detaicurrentPage = val;
                this.getDictistAll();
            },
			//高级查询每次定位到第一页
            Serch(){
                this.currentPage = 1;
                this.getDictis();
            },
			//获取最新当前页数，刷新页面
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictis();
			},
			//获取字典列表
			getDictis() {
				let para = {
				    //将我获得的当前页数  以及显示条数获取并重新赋值
					//高级查询的名字 queery内记得添加字段 跟着分页的数据一起传递
					//上面将高级查询的name 改为了keyword  下面这里重新复制也要改
                    currentPage: this.currentPage,
					pageSize: this.pageSize,
                    keyWord: this.filters.keyWord
				};
				this.listLoading = false;
				this.$http.post("/dicti",para)
					.then(result=>{
						//取出对象中对应的数据
                        this.total = result.data.total;
                        this.dictis = result.data.rows;
                        //每次成功加载关闭忙等
                        this.listLoading = false;
					})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dicti/"+row.id)
						.then(result=>{
						    //关闭忙等
                            this.listLoading = false;
                            //后端返回ajax对象
                            result = result.data;
                            if(result.success){
                                this.$message({
									message: result.msg,
									type: 'success'
								})
							}else {
                                this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                            };
							//定位到第一页
							this.currentPage = 1;
							//刷新页面
							this.getDictis();
						})
						.catch(result=>{
						    //如果删除失败  打印一下错误原因
							console.log(result);
							this.$message({
								message: "删除失败",
								type: 'error'
							})
						})

				}).catch(() => {//点击取消就不管了

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			    this.title = "修改";
				this.dictiFormVisible = true;//弹出模态框   上面为false 重新赋值就打开
				this.dictiForm = Object.assign({}, row);//克隆表格数据 回显
				this.getdeptTree();
			},
			//显示新增界面
			handleAdd: function () {
                this.title = "新增";
				this.dictiFormVisible = true;//弹出模态框
				this.dictiForm = {//清空表格数据
                    id:null,
                    name: '',
                    sn: ''
				};
			},
			//新增修改方法
			dictiSubmit: function () {
			    //validate失去焦点校验表格数据  无异常才能提交
				this.$refs.dictiForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.dictiLoading = true;//按钮忙等开启
							//将表格的数据赋值给dicti
							let dicti = Object.assign({}, this.dictiForm);
							//发送请求
							this.$http.put("/dicti",dicti).
							then(result=>{
                                this.dictiLoading = false;//按钮忙等关闭
                                result = result.data;
                                if(result.success){
                                    this.$message({
                                        message: result.msg,
                                        type: 'success'
                                    })
								}else {
                                    this.$message({
                                        message: result.msg,
                                        type: 'error'
                                    });
								}
                                this.dictiFormVisible = false;//关闭模态框
                                this.getDictis();//重新查询
							})
							.catch(result=>{
                                this.$message({
                                    message: "操作失败",
                                    type: 'error'
                                });
                            })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			    //将当前点击获得的id装入数组
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/dicti",ids)
                        .then(result=>{
                            //关闭忙等框
                            this.listLoading = false;
                            //后端返回ajax对象
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.msg,
                                    type: 'success'
                                })
                            }else {
                                this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
							};
                            //定位到第一页
                            this.currentPage = 1;
                            //刷新页面
                            this.getDictis();
                        })
                        .catch(result=>{
                            //如果删除失败  打印一下错误原因
                            this.$message({
                                message: "删除失败",
                                type: 'error'
                            })
                        })
				})
					.catch(() => {//这是点击取消

				});
			},
            //获取字典列表明细
            getDictistAll() {
                let paras = {
                    //将我获得的当前页数  以及显示条数获取并重新赋值
                    //高级查询的名字 queery内记得添加字段 跟着分页的数据一起传递
                    //上面将高级查询的name 改为了keyword  下面这里重新复制也要改
                    detaicurrentPage: this.detaicurrentPage,
                    detaipageSize: this.detaipageSize,
                    //将我取出的当前行的字典id取出用于字典明细查询
                    getdictisid:this.dictisid
                };
                this.listLoading = false;//关闭
                this.$http.post("/dictidetai",paras)
                    .then(result=>{
                        //取出对象中对应的数据
                        this.detaitotal = result.data.total;
                        this.dictistais = result.data.rows;
                        //每次成功加载关闭忙等
                        this.listLoading = false;
                    })
            },
		},
		mounted() {
			this.getDictis();
		}
	}

</script>

<style scoped>

</style>